/**
 * 功能描述
 * 
 */

define(function (require, exports, module) {
	var EditGrid = require("editgrid");
	var fetchProperties = require("../constant/fetchProperties");
	var editGridEvents = require("../handler/editGridEvents");
	var total = require("../handler/total");
	
    var instance = {
        initComponent: function (params, widget) {
        	var editGrid = new EditGrid({
        		 	parentNode: ".J-editgrid",
        		 	autoRender: false,
        		    model: {
        		        id: "editgrid",
        		        allowAdd: false,
        		        url: "api/alliancesetldetail/query",
        		        params : function() {
        		        	var pkPlanItemGroup = widget.get("form").getValue("allianceConfirm.groupReserve.planItemGroup.pkPlanItemGroup");
    						return {
    							"signUpCharge.signUpRegister.planItemGroup":pkPlanItemGroup,
    							"fetchProperties":fetchProperties.editgrid
    						};
    					},
        		        columns: [{
        		            name: "signUpCharge.signUpRegister.personalInfo.idNumber",
        		            label: "身份证号"
        		        },{
        		            name: "signUpCharge.signUpRegister.personalInfo.name",
        		            label: "姓名"
        		        },{
        		            name: "signUpCharge.signUpRegister",
        		            label: "会员证号",
        		            format: function(value, data) {
    							return value.travelContract.member.personalInfo.idNumber
    								   +"-"+value.travelContract.member.personalInfo.name
    								   +"-"+value.travelContract.productRule.product.name
    								   +"-"+value.travelContract.memberCertificates;
    			            }
        		        },{
        		            name: "signUpCharge.payable",
        		            label: "预收款",
        		            format: function(value, data) {
        		            	if(value){
        		            		return parseFloat(value).toFixed(2);	
        		            	}else{
        		            		return "";
        		            	}
    			            }
        		        },{
        		            name: "allianceCharges",
        		            label: "联盟酒店费用合计",
        		            format: function(value, data) {
        		            	if(value){
        		            		return parseFloat(value).toFixed(2);	
        		            	}else{
        		            		return "";
        		            	}
    			            },
    			            editor: {
    			                type: "text",
    			                onEdit: function(plugin, index, rowData) {
    			                    plugin.setValue(rowData.allianceCharges);
    			                },
    			                onChange: function(plugin, index, rowData) {
    			                    rowData.allianceCharges = plugin.getValue();
    			                    editGrid.update(index, rowData);
    			                    var data = {
    			                    		allianceSettlement : {
    			                    			balance : widget.get("balance"),
    			                    			paymentMethods : widget.get("paymentMethod"),
    			                    			description : widget.get("description"),
    			                    		}
    			                    };
    			                    total.total(widget,data);
    			                }
    			            }
        		        },{
        		            name: "manageCharges",
        		            label: "组团管理费",
        		            format: function(value, data) {
        		            	if(value){
        		            		return parseFloat(value).toFixed(2);
        		            	}else{
        		            		return "";
        		            	}
    			            },
    			            editor: {
    			                type: "text",
    			                onEdit: function(plugin, index, rowData) {
    			                    plugin.setValue(rowData.manageCharges);
    			                },
    			                onChange: function(plugin, index, rowData) {
    			                    rowData.manageCharges = plugin.getValue();
    			                    editGrid.update(index, rowData);
    			                    var data = {
    			                    		allianceSettlement : {
    			                    			balance : widget.get("balance"),
    			                    			paymentMethods : widget.get("paymentMethod"),
    			                    			description : widget.get("description"),
    			                    		}
    			                    };
    			                    total.total(widget,data);
    			                }
    			            }
        		        },{
        		            name: "otherCharges",
        		            label: "其他费用",
        		            format: function(value, data) {
        		            	if(value){
        		            		return parseFloat(value).toFixed(2);	
        		            	}else{
        		            		return "";
        		            	}
    			            },
    			            editor: {
    			                type: "text",
    			                onEdit: function(plugin, index, rowData) {
    			                    plugin.setValue(rowData.otherCharges);
    			                },
    			                onChange: function(plugin, index, rowData) {
    			                    rowData.otherCharges = plugin.getValue();
    			                    editGrid.update(index, rowData);
    			                    var data = {
    			                    		allianceSettlement : {
    			                    			balance : widget.get("balance"),
    			                    			paymentMethods : widget.get("paymentMethod"),
    			                    			description : widget.get("description"),
    			                    		}
    			                    };
    			                    total.total(widget,data);
    			                }
    			            }
        		        },{
        		            name: "totalCost",
        		            label: "费用合计",
        		            format: function(value, data) {
        		            	
        		            	if(!data.allianceCharges || isNaN(data.allianceCharges)){
        		            		data.allianceCharges = 0;
        		            	}
        		            	if(!data.manageCharges || isNaN(data.manageCharges)){
        		            		data.manageCharges = 0;
        		            	}
        		            	if(!data.otherCharges || isNaN(data.otherCharges)){
        		            		data.otherCharges = 0;
        		            	}
        		            	
        		            	var h = (parseFloat(data.allianceCharges)+parseFloat(data.manageCharges)+parseFloat(data.otherCharges)).toFixed(2);
        		            	return isNaN(h)?"":h;
    			            }
        		        },{
        		            name: "deductionCharges",
        		            label: "权证抵扣",
        		            format: function(value, data) {
        		            	if(value){
        		            		return parseFloat(value).toFixed(2);	
        		            	}else{
        		            		return "";
        		            	}
    			            }
        		        },{
        		            name: "balanceBilling",
        		            label: "结算金额",
        		            format: function(value, data) {
        		            	if(!data.allianceCharges || isNaN(data.allianceCharges)){
        		            		data.allianceCharges = 0;
        		            	}
        		            	if(!data.manageCharges || isNaN(data.manageCharges)){
        		            		data.manageCharges = 0;
        		            	}
        		            	if(!data.otherCharges || isNaN(data.otherCharges)){
        		            		data.otherCharges = 0;
        		            	}
        		            	var totalCost = parseFloat(data.allianceCharges) + parseFloat(data.manageCharges) + parseFloat(data.otherCharges);
        		            	var h = (parseFloat(data.signUpCharge.payable) - (parseFloat(totalCost) - parseFloat(data.deductionCharges))).toFixed(2);
    							return isNaN(h)?"":h;
    			            }
        		        },{
        		            name: "finish",
        		            label: "结算完成",
        		            format: function(value, data) {
        		            	if(typeof value == "object"){
        		            		return value.value;	
        		            	}else{
        		            		if(value){
        		            			return "是";
        		            		}else{
        		            			return "否";
        		            		}
        		            	}
    			            },
    			            editor: {
    			                type: "select",
    			                options:[{
    			                	key:"true",
    			                	value:"是"
    			                },{
    			                	key:"false",
    			                	value:"否"
    			                }],
    			                onEdit: function(plugin, index, rowData) {
    			                    plugin.setValue(rowData.finish.key);
    			                },
    			                onChange: function(plugin, index, rowData) {
    			                    rowData.finish = plugin.getData(plugin.getValue());
    			                    editGrid.update(index, rowData);
    			                    
    			                    var idata = editGrid.getData();
    			                    var num = 0;
    			                    for(var i=0;i<idata.length;i++){
    			                    	if(typeof idata[i].finish == "object" && idata[i].finish.key == "true"){
    			                    		num++;
    			                    	}else if(typeof idata[i].finish == "boolean" && idata[i].finish){
    			                    		num++;
    			                    	}
    			                    }
    			                    if(num == idata.length){
    			                    	widget.get("form").setValue("allianceSettlement.settlementDate",new Date().getTime());
    			                    }else{
    			                    	widget.get("form").setValue("allianceSettlement.settlementDate","");
    			                    }
    			                    
    			                }
    			            }
        		        }]
        		    }
        	});
        	
        	widget.set("editgrid",editGrid);
        }
    };
    module.exports = instance;
});